<?php
namespace Main\Controller;
use Think\Controller;
class IndexController extends Controller {


    public function index(){
        $ua = strtolower($_SERVER['HTTP_USER_AGENT']);

        if(strstr($ua,'micromessenger')){ //微信浏览器，进入编辑器页面
            header("location:".U('Home/Index/Index'));die;
        }

        $res = M('PageSetting')->where('id = 1')->find(); //pcsetting 

        //检查PC默认进入哪个站
        if($res['pcsetting'] == 'index'){  
            $this->baseindex(); 
        }

        if($res['pcsetting'] == 'custom_index'){
            $this->superindex();
        }

    }

    //超级首页
    private function superindex(){
        //获取站点配置
        $where['option_name'] = array(array('eq','site_name'),array('eq','seo_key'),array('eq','seo_depict'),'or');
        $opt = D('Home/Options')->getOpts($where);
        
        $result = M('LayoutList')->where(array('is_default'=>1))
                  ->field('homepage_id,is_update,content_id')->find();

        if($result['is_update']){
            $content = M('LayoutHtml')->where(array('id'=>$result['content_id']))->getField('content');
            M('LayoutList')->where(array('id'=>$result['homepage_id']))->save(array('is_update'=>0));
            F('superindex',$content);
        }else{
            $content = F('superindex');
        }


        $homepage_content = json_decode($content,1); 
        $html = $homepage_content['html'];
        $carousel = $homepage_content['carousel'];
        $this->assign(array(
            'html' => htmlspecialchars_decode($html),
            'carousel' => json_encode($carousel),
            'result' => $result,
            'opt' => $opt
        ));

        $this->display('superindex');
    }


    //简单首页
    private function baseindex(){
        $tag = I('tag',1,'intval');

        // 实例化单页模型
        $model = D('Home/Single');

        // 获取单页信息
        $info = $model->getSingleById($tag);

        // 官方网站 的单页类别ID
        $cateId = 1;

        $where = array(
            'cat_id' => $cateId,
            'status' => 1
        );

        // 获取文章列表
        $list = $model->where($where)->order('taxis desc')->field('id,title')->limit(8)->select();

        //获取站点配置
        $opt = D('Home/Options')->getOpts();
        $this->assign(array(
            'tag' => $tag,
            'opt' => $opt,
            'info' => $info,
            'list' => $list,
        ));
        $this->display('index');
    }

    //判断需要访问什么页面
    private function visitindex($ua){

        $res = M('PageSetting')->where('id = 1')->find(); //pcsetting mobilesetting

        if(strstr($ua,'mobile')){ //手机访问
            if($res['mobilesetting'] == 'pc'){  //设置了默认进PC站

                //检查PC默认进入哪个站
                if($res['pcsetting'] == 'index'){  
                    $this->baseindex(); 
                }

                if($res['pcsetting'] == 'custom_index'){
                    $this->superindex();
                }

            }

            if($res['mobilesetting'] == 'mobile'){ //设置了默认进手机端页面
                header("location:".U('Home/Index/Index'));
            }

        }else{
            if($res['pcsetting'] == 'index'){
                $this->baseindex();
            }

            if($res['pcsetting'] == 'custom_index'){
                $this->superindex();
            }
        }

    }
}